/*
 *                        _oo0oo_
 *                       o8888888o
 *                       88" . "88
 *                       (| -_- |)
 *                       0\  =  /0
 *                     ___/`---'\___
 *                   .' \\|     |// '.
 *                  / \\|||  :  |||// \
 *                 / _||||| -:- |||||- \
 *                |   | \\\  - /// |   |
 *                | \_|  ''\---/''  |_/ |
 *                \  .-\__  '-'  ___/-. /
 *              ___'. .'  /--.--\  `. .'___
 *           ."" '<  `.___\_<|>_/___.' >' "".
 *          | | :  `- \`.;`\ _ /`;.`/ - ` : | |
 *          \  \ `_.   \_ __\ /__ _/   .-` /  /
 *      =====`-.____`.___ \_____/___.-`___.-'=====
 *                        `=---='
 *
 *
 *      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 *
 *            佛祖保佑       永不宕机     永无BUG
 */
/**
 * @description: 接口
 * @param {string} to
 * @return {Function} vue实例
 */
import Vue from 'vue'
import VueRouter from 'vue-router'
import Login from '@/views/Login'
import Layout from '@/views/Layout'
import Articles from '@/views/Articles'
import Fans from '@/views/Fans'
import Comment from '@/views/Comment'
import Setting from '@/views/Setting'
import image from '@/views/image'
import Home from '@/views/Home'
import AddArticle from '@/views/AddArticle'
import NotFound from '@/views/NotFound'
import Cre from '@/views/Cre'
import EditArticle from '@/views/AddArticle/EditArticle'

import { getUserInfo } from '@/store'
Vue.use(VueRouter)
const routes = [
  {
    path: '/login',
    component: Login
  },
  {
    path: '/',
    component: Layout,
    redirect: 'home',
    children: [
      {
        path: 'articles',
        component: Articles
      },
      {
        path: 'addArticle',
        component: AddArticle
      },
      {
        path: 'fans',
        component: Fans
      },
      {
        path: 'comment',
        component: Comment
      },
      {
        path: 'setting',
        component: Setting
      },
      {
        path: 'image',
        component: image
      },
      {
        path: 'home',
        component: Home
      },
      {
        path: 'editArticle',
        component: EditArticle
      },
      {
        path: 'cro',
        component: Cre
      }
    ]
  },
  { // 404组件
    path: '*',
    component: NotFound
  }

]

const router = new VueRouter({
  routes
})

router.beforeEach((to, from, next) => {
  if (to.path === '/login') {
    next()
  } else if (to.path === '/*') {
    next()
  } else {
    const info = getUserInfo()
    if (info && info.token) {
      next()
    } else {
      next('/login')
    }
  }
})
export default router
